/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.hibernate.model.dao;

import hibernate.pojo.Chitietsanpham;
import hibernate.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author phathuynh
 */
public class AdminChiTietSanPhamDAO 
{
    //<editor-fold defaultstate="collapsed" desc="1. Adding product detail">
     public static boolean addProductdetail(Chitietsanpham productDetail){
         Session session = HibernateUtil.getSessionFactory().openSession();
         Transaction tran = null;
         try{
             tran = session.beginTransaction();
             session.save(productDetail);
             tran.commit();
         }catch(Exception hex){
             System.err.print(hex);
             tran.rollback();
             return false;
         }
         finally {
             session.close();
         }
         return true;
     }
     //</editor-fold>
     
    //<editor-fold defaultstate="collapsed" desc="2. Cập nhật chi tiết sản phẩm">
     public static boolean CapNhatChiTietSanPham(Chitietsanpham productDetail)
     {
         Session session = HibernateUtil.getSessionFactory().openSession();
         Transaction tran = null;
         try
         {
             tran = session.beginTransaction();
             session.update(productDetail);
             tran.commit();
         }
         catch(Exception hex)
         {
             System.err.print(hex);
             tran.rollback();
             return false;
         }
         finally 
         {
             session.close();
         }
         return true;
     }
     //</editor-fold> 
     
    //<editor-fold defaultstate="collapsed" desc="3. Lấy chi tiết sản phẩm theo mã chi tiết sản phẩm">
    public static Chitietsanpham LayChiTietSanPham(int maChiTietSanPham)
    {
        Chitietsanpham chiTietSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            chiTietSanPham = (Chitietsanpham) session.get(Chitietsanpham.class, maChiTietSanPham);
        }
        catch(HibernateException hex)
        {
            System.err.print(hex);
        }finally
        {
            session.close();
        }
        return chiTietSanPham;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc="4. Lấy chi tiết sản phẩm theo Mã Sản Phẩm">
    public static Chitietsanpham AdminLayChiTietSanPhamTheoMaSanPham(int maSanPham) 
    {
        Chitietsanpham ctsp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try 
        {
            String hql = "from Chitietsanpham ct where ct.sanpham.maSanPham='1'";
            Query query = session.createQuery(hql);
            query.setInteger("maSanPham", maSanPham);
            Object ob = query.uniqueResult();
            ctsp = (Chitietsanpham)ob;
            session.close();
        } catch (HibernateException ex) {
            System.out.println(ex);
        }
        return ctsp;
    }
    //</editor-fold>
}
